package com.yyy.util;

import com.sun.org.apache.bcel.internal.generic.RETURN;

import java.sql.Date;
import java.util.Calendar;

/**
 * @Author：yyy
 * @Package：com.yyy.util
 * @Project：HuTuBiill
 * @name：DateUtil
 * @Date：2024/11/10 15:46
 * @Filename：DateUtil
 */
public class DateUtil {
    static long millisecondsOfOneDay=24*60*60*1000;


    /*
    *
    * 日期转换，UtilDate转换为SqlDate
    * */
    public static Date UtilDateToSqlDate(java.util.Date date){
        return new Date(date.getTime());
    }

    /*
    * 获取今天日期，并把时分秒毫秒都设置为0
    * */
    public static java.util.Date today(){
        Calendar c=Calendar.getInstance();
        c.setTime(new java.util.Date());
        c.set(Calendar.HOUR,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        return c.getTime();

    }

    /*
    * 获取本月的第一天
    *
    * */
    public static java.util.Date monthBegin(){
        Calendar c=Calendar.getInstance();
        c.setTime(new java.util.Date());
        c.set(Calendar.DATE,1);
        c.set(Calendar.HOUR_OF_DAY,0);
        //c.set(Calendar.HOUR,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND,0);
        return c.getTime();
    }

    /*
    * 获取本月的最后一天
    * */

    public static java.util.Date monthEnd(){
        Calendar c=Calendar.getInstance();
        c.setTime(new java.util.Date());
        c.set(Calendar.DATE,1);
        c.set(Calendar.HOUR,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.add(Calendar.MONDAY,1);
        c.add(Calendar.DATE,-1);
        return c.getTime();
    }

    /*
    * 获取本月一共有多少天
    *
    * */
    public static int thisMonthTotalDay(){
        long lastDayMilliSeconds=monthEnd().getTime();
        long firstDayMillSeconds=monthBegin().getTime();
        return (int)((lastDayMilliSeconds-firstDayMillSeconds)/millisecondsOfOneDay)+1;

    }

    /*
    * 获取本月还剩多少天
    * */
    public static int thisMonthLeftDay(){
        long lastDayMilliSeconds=monthEnd().getTime();
        long toDayMilliSeconds=today().getTime();
        return (int)((lastDayMilliSeconds-toDayMilliSeconds)/millisecondsOfOneDay)+1;
    }
    //Fri Nov 01 00:00:00 CST 2024
    //Fri Nov 01 12:00:00 CST 2024
    public static void main(String[] args) {
        System.out.println(today());
        System.out.println(monthBegin());
        System.out.println(monthEnd());
        System.out.println(thisMonthTotalDay());
        System.out.println(thisMonthLeftDay());
    }











}
